﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
using LdExpress;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    public string ContainerPage = "./ProcessJob.aspx";
    public string strCondition = "";
    public int NumOfItemsPerPage = 1000;
    public string strPager = "";
    public string strError = "";
    public string strMess = "";
    public string type = "";
    public string strProcessName = "";
    public int nPermissions = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        int nPage = 1;
        string strPageID = "page_number";
        if (Request.QueryString["type"] != "")
            type = Request.QueryString["type"].ToString();
        else
            type = "0";
        switch (type)
        {
            case "1":
                strProcessName = "CUS";
                break;
            case "2":
                strProcessName = "DOC";
                break;
            case "3":
                strProcessName = "SALES";
                break;
            case "4":
                strProcessName = "ACCNT";
                break;
        }
        try
        {

            if (Request.QueryString[strPageID] != null)
                nPage = int.Parse(Request.QueryString[strPageID]);
        }
        catch
        {
            nPage = 1;
        }
        if (nPage <= 0) nPage = 1;
        if (Session["changLoadingPort"] == null)
            Session["changLoadingPort"] = "";
        if (Session["logined_id"] != null)
        {
            if (type == "1")
            {
                if (Session["ProcessCus"] != "") nPermissions = int.Parse(Session["ProcessCus"].ToString());
            }
            else if (type == "2")
            {
                if (Session["ProcessDoc"] != "") nPermissions = int.Parse(Session["ProcessDoc"].ToString());
            }
            else if (type == "3")
            {
                if (Session["ProcessSales"] != "") nPermissions = int.Parse(Session["ProcessSales"].ToString());
            }
            else if (type == "4")
            {
                if (Session["ProcessAccnt"] != "") nPermissions = int.Parse(Session["ProcessAccnt"].ToString());
            }
            if (nPermissions <= 0) Response.Redirect("Login.aspx");
        }
        else
            Response.Redirect("Login.aspx");

        if (!Page.IsPostBack)
        {
            loadDropdownLoadingPort();
            if (Request.QueryString["action"] == "delete" && nPermissions > 7)
            {
                ProcessJob obj = new ProcessJob();
                obj.ID = long.Parse(Request.QueryString["PortsID"]);
                obj.Load(GV.cn, GV.trans);
                obj.Delete(GV.cn, GV.trans);
                Response.Redirect("ProcessJob.aspx?type=" + type + ((Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "") ? "page_number=" + Request.QueryString["page_number"].ToString() : ""));
            }
            else
            {
                if (Request.QueryString["ProcessID"] != null)
                {
                    loadProcessJob();
                }
            }

        }

    }
    protected void loadProcessJob()
    {
        ProcessJob obj = new ProcessJob();
        obj.ID = long.Parse(Request.QueryString["ProcessID"]);
        obj.Load(GV.cn, GV.trans);
        txtStepsJob.Text = HttpUtility.HtmlDecode(obj.StepsJob);
        txtSort.Text = obj.Sort.ToString();
        txtLoadingPort.Text = obj.LoadingPort;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        strError = "";
        strMess = "";
        if (txtStepsJob.Text == "")
        {
            strError = "NotBlank";
            strMess = "Các bước công việc không được để trống!";
            lblerror.Text = strMess;
            return;
        }
        string strType = "";
        string strParent = "";
        ProcessJob obj = new ProcessJob();
        obj.ID = -1;
        if (Request.QueryString["ProcessID"] != null)
        {
            obj.ID = long.Parse(Request.QueryString["ProcessID"]);
            obj.Load(GV.cn, GV.trans);
        }
        if (Request.QueryString["type"] != null)
        {
            obj.Type = long.Parse(Request.QueryString["type"].ToString());
            strType = "type=" + Request.QueryString["type"].ToString();
        }
        if (Request.QueryString["ParentID"] != null)
        {
            obj.Parent = int.Parse(Request.QueryString["ParentID"].ToString());
            strParent = "&ParentID=" + Request.QueryString["ParentID"].ToString();
        }
        obj.StepsJob = txtStepsJob.Text.Trim();
        obj.LoadingPort = txtLoadingPort.Text.Trim();
        //obj.Sort = long.Parse(txtSort.Text.Trim());
        long.TryParse(txtSort.Text.Trim(), out obj.Sort);
        obj.LastUpdate = DateTime.Now;
        Response.Write("strError" + strError);
        if (strError == "")
        {
            obj.Save(GV.cn, GV.trans);
            Response.Redirect("ProcessJob.aspx?" + strType + strParent + ((Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "") ? "&page_number=" + Request.QueryString["page_number"].ToString() : ""));
        }        
    }
    protected void LoadStepJob(long ParentID, long deep)
    {
        string retVal = "";
        DataSet ds = null;
        DataSet dsChild = null;
        string sql = "";
        string index = "";
        int nNumOfPages = 0;
        int NumOfItemsPerPage = 100;
        int PageNo = 1;
        deep++;
        if (Request.QueryString["type"] != null)
            strCondition = " and Type=" + Request.QueryString["type"].ToString();
        else
            strCondition = " and 0=1";

        string strConditionParent = strCondition + " AND Parent=" + ParentID;
        if (Session["changLoadingPort"].ToString().Length > 0)
        {
            if (Session["changLoadingPort"].ToString() != "All" && ParentID == 0)
            {
                strConditionParent += " AND LoadingPort='" + Session["changLoadingPort"].ToString() + "'";
            }
            ProcessJob obj = new ProcessJob();
            obj.GetList(strConditionParent, "Sort ASC,ID ASC", NumOfItemsPerPage, PageNo, out nNumOfPages, out ds, GV.cn, GV.trans);

            Response.Write("<tbody>");
            if (ds != null)
            {
                int count = 0;

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    count++;
                    long strID = int.Parse(dr["ID"].ToString());
                    if (ParentID > 0)
                        index = count.ToString() + " - ";
                    string strStepJob = dr["StepsJob"].ToString();
                    ProcessJob objChild = new ProcessJob();
                    string strLevel = "";
                    if (deep == 1)
                        strLevel = "style=\"background-color:#ececec;\"";
                    string strConditionChild = strCondition + " AND Parent=" + strID;
                    objChild.GetList(strConditionChild, "Sort ASC,ID ASC", NumOfItemsPerPage, PageNo, out nNumOfPages, out dsChild, GV.cn, GV.trans);
                    long spaceleft = deep * 15;
                    string strLastUpdate = dr["LastUpdate"].ToString();
                    if (dr["LastUpdate"].ToString() != "")
                    {
                        strLastUpdate = DateTime.Parse(dr["LastUpdate"].ToString()).ToString("ddMMyy");
                    }
                    string bdelete = "yes";
                    if (dsChild.Tables[0].Rows.Count > 0)
                        bdelete = "no";
                    string strAction = "";
                    if (nPermissions >= 3)
                        strAction = "<a href=\"ProcessJob.aspx?type=" + type + "&ProcessID=" + strID + ((Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "") ? "&page_number=" + Request.QueryString["page_number"].ToString() : "") + "\">Edit</a>";
                    if (nPermissions == 7)
                    {
                        strAction += " - <a href=\"javascript:doDelete('" + System.Web.HttpUtility.HtmlEncode(strID.ToString()) + "'," + System.Web.HttpUtility.HtmlEncode(type) + ",'" + bdelete + "')\">" + "Delete</a>";
                        strAction += " - <a href=\"ProcessJob.aspx?type=" + type + "&ParentID=" + strID + ((Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "") ? "&page_number=" + Request.QueryString["page_number"].ToString() : "") + "\">Add Sub</a>";
                    }

                    if (dsChild.Tables[0].Rows.Count > 0)
                    {
                        Response.Write("<tr " + strLevel + ">");
                        Response.Write("<td><p class='wrapword'  style=\"padding-left: " + spaceleft.ToString() + "px;\">" + index + strStepJob + "</td>");
                        Response.Write("<td style=\"text-align:center;width:50px;\">" + strLastUpdate + "</td>");
                        Response.Write("<td style=\"text-align:center;width:120px;\">" + strAction + "</td>");
                        Response.Write("</tr>");
                        LoadStepJob(strID, deep);
                    }
                    else
                    {
                        Response.Write("<tr " + strLevel + ">");
                        Response.Write("<td><p class='wrapword' style=\"padding-left: " + spaceleft.ToString() + "px;\">" + index + strStepJob + "</td>");
                        Response.Write("<td style=\"text-align:center;width:50px;\">" + strLastUpdate + "</td>");
                        Response.Write("<td style=\"text-align:center;width:120px;\">" + strAction + "</td>");
                        Response.Write("</tr>");
                    }

                }

            }
            Response.Write("</tbody>");
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("ProcessJob.aspx?type=" + Request.QueryString["type"].ToString() + ((Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "") ? "&page_number=" + Request.QueryString["page_number"].ToString() : ""));
    }

    protected void loadDropdownLoadingPort()
    {
        string sql = "";
        DataSet ds = null;
        string condition = "";

        sql = "SELECT DISTINCT LoadingPort FROM tbl_processjob WHERE Parent=0 And Type=" + Request.QueryString["type"].ToString();
        ds = DBModule.ExecuteQuery(sql, GV.cn, GV.trans);
        DataTable dtb = ds.Tables[0];
        drLoadingPort.DataSource = dtb;
        drLoadingPort.DataValueField = dtb.Columns[0].ToString();
        drLoadingPort.DataTextField = dtb.Columns[0].ToString();
        drLoadingPort.DataBind();
        drLoadingPort.Items.Insert(0, new ListItem("LOADING PORT", ""));
        drLoadingPort.Items.Insert(1, new ListItem("All", "All"));
        if (Session["changLoadingPort"] != null && ds.Tables[0].Rows.Count > 0)
        {
            try
            {
                drLoadingPort.SelectedValue = Session["changLoadingPort"].ToString();
            }
            catch { }
        }
    }
    protected void drLoadingPort_Selected(object sender, EventArgs e)
    {
        Session["changLoadingPort"] = drLoadingPort.SelectedValue;
    }
}
